/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ADSLPanel.java
 *
 * Created on 02-ago-2011, 19:41:02
 */
package org.rcs.adslmonitor;

import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.Window;
import java.util.EventObject;
import javax.swing.SwingUtilities;
import org.rcs.adslmonitor.routers.TPLink8970;

/**
 *
 * @author pablo
 */
public class ADSLPanel extends javax.swing.JPanel implements ADSLStatusListener {

  private ADSLStatus status;

  /**
   * Creates new form ADSLPanel
   */
  public ADSLPanel() {
    initComponents();

    status = new TPLink8970() ;
    //status = new ZTEZXV10W300();

    routerModelLabel.setText("<html><b>" + status.getRouterName() + "</b></html>");

    if (!status.hasError())
      refresh();
    else
      summaryLabel.setText("<html>" + status.getLastError() + "</html>");

    status.addListener(this);
    status.addListener((ADSLStatusListener) statusHistory);
    status.setRefreshRate(5);
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    hideButton = new javax.swing.JButton();
    refreshButton = new javax.swing.JButton();
    statusLabel = new javax.swing.JLabel();
    statusValueLabel = new javax.swing.JLabel();
    modeLabel = new javax.swing.JLabel();
    modeValueLabel = new javax.swing.JLabel();
    summaryLabel = new javax.swing.JLabel();
    configButton = new javax.swing.JButton();
    configButton.setVisible(false);
    routerModelLabel = new javax.swing.JLabel();
    statusHistory = new StatusHistoryPanel() ;
    exitButton = new javax.swing.JButton();

    setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    hideButton.setText("-");
    hideButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
    hideButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        hideButtonActionPerformed(evt);
      }
    });

    refreshButton.setText("R");
    refreshButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
    refreshButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        refreshButtonActionPerformed(evt);
      }
    });

    statusLabel.setText("<html><b>Estado :</b></html>");

    statusValueLabel.setText("Desconectado");

    modeLabel.setText("<html><b>Modo :</b></html>");

    modeValueLabel.setText("Inactivo");

    summaryLabel.setText("Sin conexión");

    configButton.setText("...");
    configButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
    configButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        configButtonActionPerformed(evt);
      }
    });

    routerModelLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    routerModelLabel.setText("Router desconocido");

    statusHistory.setBackground(new java.awt.Color(0, 0, 0));

    javax.swing.GroupLayout statusHistoryLayout = new javax.swing.GroupLayout(statusHistory);
    statusHistory.setLayout(statusHistoryLayout);
    statusHistoryLayout.setHorizontalGroup(
      statusHistoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 100, Short.MAX_VALUE)
    );
    statusHistoryLayout.setVerticalGroup(
      statusHistoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 83, Short.MAX_VALUE)
    );

    exitButton.setText("X");
    exitButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
    exitButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        exitButtonActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(routerModelLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(hideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(layout.createSequentialGroup()
            .addComponent(modeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(modeValueLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(configButton)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(refreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(layout.createSequentialGroup()
            .addComponent(summaryLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(statusHistory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(layout.createSequentialGroup()
            .addComponent(statusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(statusValueLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(routerModelLabel)
          .addComponent(hideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(statusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(statusValueLabel))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
          .addComponent(statusHistory, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(summaryLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(modeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(modeValueLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(refreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(configButton, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap())
    );
  }// </editor-fold>//GEN-END:initComponents

    private void hideButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hideButtonActionPerformed
      Window window = SwingUtilities.getWindowAncestor(this);
      window.setVisible(false);
}//GEN-LAST:event_hideButtonActionPerformed

  private void updateControls() {
    if (status.hasError()) {
      statusValueLabel.setText("Error");
      modeValueLabel.setText("");
      summaryLabel.setText("<html>" + status.getLastError() + "</html>");
    } else {
      statusValueLabel.setText(status.get(ADSLStatus.KEYS.ADSL_LINE_STATUS));
      modeValueLabel.setText(status.get(ADSLStatus.KEYS.ADSL_MODE));

      summaryLabel.setText(
              String.format("<html><table cellspacing=1>"
              + "<tr><td></td><td bgcolor='#AAAAAA' align='center'><b>V/M (kbps)</b></td><td  bgcolor='#AAAAAA' align='center'><b>A/R (db)</b></td></tr>"
              + "<tr><td bgcolor='#AAAAAA'><b>B</font></b></td><td align ='right' bgcolor='#CCCCCC'>%s/%s</td><td align ='right' bgcolor='#CCCCCC'>%s/%s</td></tr>"
              + "<tr><td bgcolor='#AAAAAA'><b>S</b></td><td align ='right' bgcolor='#DDDDDD'>%s/%s</td><td align ='right' bgcolor='#DDDDDD'>%s/%s</td></tr>"
              + "<table></html>",
              status.get(ADSLStatus.KEYS.DOWN_STREAM), status.get(ADSLStatus.KEYS.ATTAINABLE_DOWN_RATE),
              status.get(ADSLStatus.KEYS.ATTENUATION_DOWN_STREAM), status.get(ADSLStatus.KEYS.SNR_MARGIN_DOWN_STREAM),
              status.get(ADSLStatus.KEYS.UP_STREAM), status.get(ADSLStatus.KEYS.ATTAINABLE_UP_RATE),
              status.get(ADSLStatus.KEYS.ATTENUATION_UP_STREAM), status.get(ADSLStatus.KEYS.SNR_MARGIN_UP_STREAM)));

    }

    Window window = SwingUtilities.getWindowAncestor(this);

    if (null != window)
      window.pack();
  }

  private void refresh() {
    status.refresh();
    updateControls();
  }

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
      refresh();
}//GEN-LAST:event_refreshButtonActionPerformed

    private void configButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configButtonActionPerformed
      // TODO add your handling code here:
    }//GEN-LAST:event_configButtonActionPerformed

  private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
    Window window = SwingUtilities.getWindowAncestor(this);
    window.setVisible(false);
    status.stopRefreshRate();
    window.dispose();

    SystemTray systemTray = SystemTray.getSystemTray();
    TrayIcon[] icons = systemTray.getTrayIcons();

    for (TrayIcon tray : icons)
      systemTray.remove(tray);

    System.exit(0);
  }//GEN-LAST:event_exitButtonActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton configButton;
  private javax.swing.JButton exitButton;
  private javax.swing.JButton hideButton;
  private javax.swing.JLabel modeLabel;
  private javax.swing.JLabel modeValueLabel;
  private javax.swing.JButton refreshButton;
  private javax.swing.JLabel routerModelLabel;
  private javax.swing.JPanel statusHistory;
  private javax.swing.JLabel statusLabel;
  private javax.swing.JLabel statusValueLabel;
  private javax.swing.JLabel summaryLabel;
  // End of variables declaration//GEN-END:variables

  public void statusChanged(EventObject evt) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        updateControls();
      }
    });
  }

  public ADSLStatus getADSLStatus() {
    return status;
  }
}
